package cc.wfu.trigger.listener;

import cc.wfu.domain.user.service.IUserService;
import cc.wfu.types.event.BaseEvent;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 监听用户上次登录信息 - 更新用户信息
 * @Date: 2024-05-20-21:04
 */
@Slf4j
@Component
public class UserLastLoginCustomer {

    @Value("${spring.rabbitmq.topic.user_last_login_time}")
    private String topic;

    @Resource
    private IUserService userService;

    @RabbitListener(queuesToDeclare = @Queue(value = "user_last_login_time"))
    public void listener(String message) {
        try {
            log.info("监听用户上次登录信息 topic: {} message: {}", topic, message);
            // 转换对象
            BaseEvent.EventMessage<String> eventMessage =
                    JSON.parseObject(message, new TypeReference<BaseEvent.EventMessage<String>>() {}.getType());
            String email = eventMessage.getData();
            // 修改操作
            userService.updateLastLoginTime(email);
        } catch (Exception e) {
            log.error("监听用户上次登录信息，消费失败 topic: {} message: {}", topic, message, e);
//            throw e;  // 不用抛异常，消费失败也无伤大雅
        }
    }
}
